﻿using LiveShopvnext.Images;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Entities;
using Volo.Abp.Domain.Repositories;

namespace LiveShopvnext.Imges
{
    public class ImagesAppService : ApplicationService , IImagesAppService
    {
        private readonly IRepository<Imges.Image, int> _image;
        public ImagesAppService(IRepository<Imges.Image, int> images)
        {
            _image = images;
        }

        #region 添加
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="createUpdate"></param>
        /// <returns></returns>
        /// 
        [HttpPost]
        public async Task<int> CreateImg(CreateUpdateImageDto createUpdate)
        {
            //var getimg = (from a in _image
            //              where a.imageSId == createUpdate.imageSId
            //              select a.Id).ToArray();

            //if (getimg.Length>0)
            //{
            //    await _image.DeleteManyAsync(getimg);
            //}

            List<Image> list = new List<Image>();

            foreach (var item in createUpdate.imageURL)
            {
                list.Add(new Image { imageURL = item, imageCategroy = createUpdate.imageCategroy, imageSId = createUpdate.imageSId });
            }
            await _image.InsertManyAsync(list);
            return 1;
          
        }
        #endregion

        #region 图片显示
        /// <summary>
        /// 显示
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet]
        public PagedResultDto<ImageDto> GetImage(ImageListDto input)
        {
            var getimg = from a in _image
                         select new ImageDto {
                             imageURL = a.imageURL,
                             imageSId = a.imageSId,
                             imageCategroy = a.imageCategroy,
                             imagedelete=a.imagedelete
                         };

            var list = getimg.WhereIf(input.imageSId>0,c=>c.imageSId==input.imageSId&& c.imagedelete!=1).ToList();
            var totalcount= list.Count;
            return new PagedResultDto<ImageDto>(totalcount, list);
        }
        #endregion

        #region 批量删除
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<int> DeleteImage(List<int> ids)
        {
            foreach (var id in ids)
            {
                await _image.DeleteAsync(id);
            }
            return 1;
        }
        #endregion



    }
}
